﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmorCanino.Web.Models.Models;
using AmorCanino.Common;

namespace AmorCanino.Web.Models.Services
{
    public class MessagingService : BaseService, IMessagingService
    {

        public void SetMessages(AmorCanino.Web.Models.Models.Communication message,
            int originatorUser, int destinationUser)
        {
            var db = new AmorCaninoDBDataContext();
            var existingMessage = (from c in db.Communications
                                  where c.OriginatorID == originatorUser &&
                                  c.TargetID == destinationUser
                                  select c).SingleOrDefault();

            if (existingMessage == null)
            {
                //New one
                db.Communications.InsertOnSubmit(new Communication()
                {
                    TargetID = destinationUser,
                    OriginatorID = originatorUser,
                    TextMessage = new JSonParser().Serialize(new Chat()
                    {
                        Messages = new List<Models.Communication>() { message }
                    }),
                    ReadedByOriginator = true,
                    ReadedByTarget = false
                });
            }
            else
            {
                //Update message
                var parser = new JSonParser();
                var currentChat = parser.Deserialize<Chat>(existingMessage.TextMessage);
                currentChat.Messages.Add(message);
                existingMessage.TextMessage = parser.Serialize(currentChat);
                existingMessage.ReadedByOriginator = existingMessage.OriginatorID == originatorUser;
                existingMessage.ReadedByTarget = existingMessage.TargetID == originatorUser;
            }

            db.SubmitChanges();
        }

        public Chat GetMessage(int originatorUser, int destinationUser, int myId)
        {
            var db = new AmorCaninoDBDataContext();
            var existingMessages = (from c in db.Communications
                                    where c.OriginatorID == originatorUser &&
                                    c.TargetID == destinationUser
                                    select c).SingleOrDefault();

            if (originatorUser == myId)
                existingMessages.ReadedByOriginator = true;

            if (destinationUser == myId)
                existingMessages.ReadedByTarget = true;

            db.SubmitChanges();

            return new JSonParser().Deserialize<Chat>(existingMessages.TextMessage);
        }

        public List<FullComunication> GetAllMessages(int myId)
        {
            var db = new AmorCaninoDBDataContext();
            var existingMessages = (from c in db.Communications
                                   where c.OriginatorID == myId ||
                                   c.TargetID == myId
                                   select c);

            var chats = new List<FullComunication>();
            var parser = new JSonParser();

            foreach (var item in existingMessages)
            {
                chats.Add(new FullComunication()
                {
                    ChatContent = parser.Deserialize<Chat>(item.TextMessage),
                    Originator = item.OriginatorID,
                    Target = item.TargetID,
                    IsReaded = (item.OriginatorID == myId && item.ReadedByOriginator == false)
                        || (item.TargetID == myId && item.ReadedByTarget == false)
                });
            }

            return chats;
        }


        public int GetUnreadedMessagesCount(int myId)
        {
            var db = new AmorCaninoDBDataContext();
            var count = db.Communications.Count(c => c.OriginatorID == myId && c.ReadedByOriginator == false);
            count += db.Communications.Count(c => c.TargetID == myId && c.ReadedByTarget == false);
            return count;
        }
    }
}